package models

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

func init() {
	// 注册驱动～
	err := orm.RegisterDriver("mysql", orm.DRMySQL)
	if err != nil {
		fmt.Printf("error : %v", err)
		return
	}
	// 读取数据库相关配置
	dbname := beego.AppConfig.String("database.name")
	host := beego.AppConfig.String("database.host")
	port := beego.AppConfig.String("database.port")
	username := beego.AppConfig.String("database.username")
	password := beego.AppConfig.String("database.password")

	url := username + ":" + password + "@tcp(" + host + ":" + port + ")/" + dbname + "?charset=utf8"
	// 注册DB
	err = orm.RegisterDataBase("default", "mysql", url)
	if err != nil {
		fmt.Printf("error : %v", err)
		return
	}

	// 注册modal
	orm.RegisterModel(
		//new(FileUploadDetail),
		new(FileUpload),
		new(FileCategory),
	)

	//// 自动建表
	//err = orm.RunSyncdb("default", false, true)
	if err != nil {
		fmt.Printf("error : %v", err)
		return
	}
	orm.RunCommand()
}
